package com.carleasoft.mps.cms.business.education.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.article.constants.ExceptionCMSEnum;
import com.carleasoft.mps.cms.business.education.bean.entity.EducationEntity;
import com.carleasoft.mps.cms.business.education.bean.entity.EducationCollectEntity;
import com.carleasoft.mps.cms.business.education.bean.vo.EducationTreeVo;
import com.carleasoft.mps.cms.business.education.dao.EducationMapper;
import com.carleasoft.mps.cms.business.education.service.EducationService;
import com.carleasoft.mps.cms.business.education.service.EduCollectService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: yangHao
 * @createDate:2023/6/6
 * @description:
 */
@Service
public class EducationServiceImpl extends ServiceImpl<EducationMapper, EducationEntity> implements EducationService {
    @Autowired
    private EduCollectService eduCollectService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveEducation(EducationEntity educationEntity) {
        boolean isOK = false;
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        educationEntity.setCreateTime(LocalDateTime.now());
        educationEntity.setCreatePersonCode(user.getUserCode());
        educationEntity.setCreatePersonName(user.getUserName());
        if (StrUtil.isNotBlank(educationEntity.getEducationCode())) {
            EducationEntity one = this.getOne(Wrappers.<EducationEntity>lambdaQuery()
                    .eq(EducationEntity::getEducationCode, educationEntity.getEducationCode()));
            educationEntity.setId(one.getId());
            isOK = this.updateById(educationEntity);
        } else {
            educationEntity.setEducationCode(IdWorker.getIdStr());
            isOK = this.save(educationEntity);
        }
        return isOK;
    }

    @Override
    public List<EducationEntity> select() {
        return this.list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(EducationEntity educationEntity) {
        int count = eduCollectService.count(Wrappers.<EducationCollectEntity>lambdaQuery()
                .eq(EducationCollectEntity::getEducation, educationEntity.getEducationCode()));
        if (count > 0) {
            throw new BusinessException(ExceptionCMSEnum.EXITS_NO);
        }
        return this.remove(Wrappers.<EducationEntity>lambdaQuery()
                .eq(EducationEntity::getEducationCode, educationEntity.getEducationCode()));
    }

    @Override
    public List<EducationTreeVo> getTree() {
        List<EducationTreeVo> educationTreeVos = new ArrayList<>();
        List<EducationEntity> educationEntities = this.list();
        List<String> codes = educationEntities.stream().map(EducationEntity::getEducationCode).collect(Collectors.toList());
        if(codes.size() > 0){
            List<EducationCollectEntity> reasourceCollectEntities = eduCollectService.list(Wrappers.<EducationCollectEntity>lambdaQuery()
                    .in(EducationCollectEntity::getEducation, codes));
            Map<String, List<EducationCollectEntity>> map = new HashMap<>();
            if (CollUtil.isNotEmpty(reasourceCollectEntities)) {
                map = reasourceCollectEntities.stream().collect(Collectors.groupingBy(EducationCollectEntity::getEducation));
            }
            for (EducationEntity educationEntity : educationEntities) {
                EducationTreeVo educationTreeVo = new EducationTreeVo();
                educationTreeVo.setType(1);
                educationTreeVo.setImage(educationEntity.getImage());
                educationTreeVo.setCode(educationEntity.getEducationCode());
                educationTreeVo.setName(educationEntity.getEducationName());
                educationTreeVo.setIsShow(educationEntity.getIsShow());
                educationTreeVo.setCreateTime(educationEntity.getCreateTime());
                educationTreeVo.setCreatePersonName(educationEntity.getCreatePersonName());
                educationTreeVo.setCreatePersonCode(educationEntity.getCreatePersonCode());
                if (map.containsKey(educationEntity.getEducationCode())) {
                    List<EducationCollectEntity> collectEntities = map.get(educationEntity.getEducationCode());
                    List<EducationTreeVo> educationTreeVoList = new ArrayList<>();
                    for (EducationCollectEntity collectEntity : collectEntities) {
                        EducationTreeVo educationTree = new EducationTreeVo();
                        educationTree.setType(2);
                        educationTree.setParentCode(educationTreeVo.getCode());
                        educationTree.setCode(collectEntity.getCategoryCode());
                        educationTree.setName(collectEntity.getCategoryName());
                        educationTree.setCreateTime(collectEntity.getCreateTime());
                        educationTree.setCreatePersonCode(collectEntity.getCreatePersonCode());
                        educationTree.setCreatePersonName(collectEntity.getCreatePersonName());
                        educationTreeVoList.add(educationTree);
                    }
                    educationTreeVo.setChildren(educationTreeVoList);
                }
                educationTreeVos.add(educationTreeVo);
            }
        }
        return educationTreeVos;
    }
}
